/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.dto.MyOrderDto;
import com.yami.shop.bean.app.dto.MyOrderItemDto;
import com.yami.shop.bean.model.Order;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.PageAdapter;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.OrderMapper;
import com.yami.shop.service.MyOrderService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author lgh on 2018/09/15.
 */
@Service
@AllArgsConstructor
public class MyOrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements MyOrderService {

    private final OrderMapper orderMapper;

    @Override
    public IPage<MyOrderDto> pageMyOrderByUserIdAndStatus(Page<MyOrderDto> page, String userId, Integer status, Integer lang) {
        List<MyOrderDto> myOrderDtos = orderMapper.listMyOrderByUserIdAndStatus(new PageAdapter(page), userId, status,lang);
        for (MyOrderDto myOrderDto : myOrderDtos) {
            if(StrUtil.isBlank(myOrderDto.getShopName())){
                myOrderDto.setShopName(Constant.PLATFORM_SHOP_NAME);
            }
            List<MyOrderItemDto> orderItemDtos = myOrderDto.getOrderItemDtos();
            int score = orderItemDtos.stream().mapToInt(MyOrderItemDto::getUseScore).sum();
            myOrderDto.setUserScore(score);
        }
        page.setRecords(myOrderDtos);
        page.setTotal(orderMapper.countMyOrderByUserIdAndStatus(userId, status, null,null));
//        Order order = getById(1352);
//        // 消息推送-自提点核销通知
//        notifyTemplateService.writeOffOrder("lalala",order)
        return page;
    }

    @Override
    public IPage<MyOrderDto> pageMyOrderByParams(Page<MyOrderDto> page, String userId, Integer status,
                                                 String orderName, Integer orderTimeStatus, Integer orderType, String orderNumber, Integer dbLang, Long shopId) {
        String preTime="";
        if (Objects.equals(1,orderTimeStatus)) {
            preTime = getStatetime(7);
        }
        if (Objects.equals(2,orderTimeStatus) || Objects.equals(3,orderTimeStatus)) {
            preTime = getReduceMonth(30);
        }
        List<MyOrderDto> myOrderDtoList = orderMapper.listMyOrderByParams(new PageAdapter(page), userId, status, orderName,
                orderTimeStatus, preTime, orderType, orderNumber,dbLang, shopId);
        for (MyOrderDto myOrderDto: myOrderDtoList){
            if (Objects.equals(myOrderDto.getShopId(),Constant.PLATFORM_SHOP_ID)){
                myOrderDto.setShopName(Constant.PLATFORM_SHOP_NAME);
            }
            List<MyOrderItemDto> orderItemDtos = myOrderDto.getOrderItemDtos();
            int score = orderItemDtos.stream().mapToInt(MyOrderItemDto::getUseScore).sum();
            myOrderDto.setUserScore(score);
        }
        page.setRecords(myOrderDtoList);
        page.setTotal(orderMapper.countMyOrderByParams(userId, status,orderName,orderTimeStatus,preTime,orderType,orderNumber, shopId));
        return page;
    }

    @Override
    public IPage<MyOrderDto> myOrderComment(PageParam<MyOrderDto> page, String userId, Integer isComm) {
        List<MyOrderDto> myOrderDtos = orderMapper.orderCommentByUserIdAndStatus(new PageAdapter(page), userId, isComm);
        for (MyOrderDto myOrderDto : myOrderDtos) {
            if(StrUtil.isBlank(myOrderDto.getShopName())){
                myOrderDto.setShopName(Constant.PLATFORM_SHOP_NAME);
            }
        }
        page.setRecords(myOrderDtos);
        page.setTotal(orderMapper.countMyOrderByUserIdAndComm(userId, isComm));
        return page;
    }

    @Override
    public IPage<MyOrderItemDto> myOrderItemsComment(PageParam<MyOrderItemDto> page, String userId, Integer isComm) {
        List<MyOrderItemDto> myOrderDtos = orderMapper.orderItemCommentByUserIdAndStatus(new PageAdapter(page), userId, isComm);
        page.setRecords(myOrderDtos);
        page.setTotal(orderMapper.countOrderItemComment(userId, isComm));
        return page;
    }

    @Override
    public List<MyOrderDto> getOrderListByStationId(Long stationId, String userId) {
        return orderMapper.getOrderListByStationId(stationId, userId);
    }

    /**
     * 获取当前时间前7天的时间点
     */
    private String getStatetime(int reduce){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        // reduce = -7 前7天
        c.add(Calendar.DATE, - reduce);
        Date monday = c.getTime();
        String preMonday = sdf.format(monday);
        return preMonday;
    }
    /**
     * 获取当前时间 前三个月的时间点
     */
    private String getReduceMonth(int reduce){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        // reduce = -7 前7天
        c.add(Calendar.MONTH, - reduce);
        Date month = c.getTime();
        String priMonth = sdf.format(month);
        return priMonth;
    }
}
